const app = getApp();
const pageSize = 15;
const statusOptions = [{
        value: "",
        name: "全部"
    },
    {
        value: 2,
        name: "已安装"
    },
    {
        value: 1,
        name: "未安装"
    }
];
Page({

    /**
     * 页面的初始数据
     */
    data: {
        total: 0,
        isFilter: false,
        libraryList: [],
        nomore: false,
        loadmore: false,
        page: 1, //当前页数
        drawer: false,
        statusOptions, //安装状态选项
        mainboardOptions: [], //主板类型选项
        partners: [], //合伙人
        conditions: {
            partnerIndex: "", //合伙人索引
            statusIndex: "", //安装装填选中索引
            mainboardIndex: "", //主板类型索引   
            startTime: "",
            endTime: "",
            eqSn: "",
        },
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getlibraryList();
        this.getMainBoardType();
        this.getPartnerList();
    },


    reloadData() {
        this.setData({
            page: 1,
            libraryList: [],
            nomore: false,
            loadmore: false,
            total: 0,
        })
        wx.showLoading();
        this.getlibraryList();
    },


    
    getPartnerList() {
        app.ajax({
            url: "Manage/Equipment/getPartnerList",
            success: res => {
                if (res.data.code > 0) {
                    this.setData({
                        partners: res.data.data
                    })
                }
            }
        })
    },

    showDrawer() {
        this.condition = {
            ...this.data.conditions
        };
        this.setData({
            drawer: true,
        })
    },



    resetDrawer() {
        const conditions = this.data.conditions;
        for (let x in conditions) {
            conditions[x] = "";
        }
        this.setData({
            conditions,
        })
    },

    confirmDrawer() {
        let isFilter = false;
        const conditions = this.data.conditions;
        for (let x in conditions) {
            conditions[x] !== "" && (isFilter = true);
        }
        this.setData({
            isFilter
        })
        delete(this.condition);
        this.reloadData();
    },

    onPullDownRefresh: function () {
        this.reloadData();
    },

    closeDrawer() {
        this.setData({
            drawer: false,
            conditions: this.condition
        })
        delete(this.condition)
    },

    handleCondition(e) {
        const name = e.currentTarget.dataset.name;
        this.setData({
            [`conditions.${name}`]: e.detail.value
        })
    },


    getMainBoardType() {
        app.ajax({
            url: "Common/Common/getEquipmentMainboard",
            success: res => {
                const options = [{
                    name: "全部",
                    number: ""
                }]
                if (res.data.code == 1000) {
                    this.setData({
                        mainboardOptions: options.concat(res.data.data)
                    })
                    console.log(res.data.data)
                }
            }
        })
    },

    onReachBottom: function () {
        if (this.data.loadmore) {
            this.setData({
                page: this.data.page + 1
            })
            this.getlibraryList();
        }
    },
    copyNumber(e) {
        const sn = e.currentTarget.dataset.sn;
        wx.setClipboardData({
            data: sn,
        })
    },

    //获取卡库列表
    getlibraryList() {
        var that = this;
        app.ajax({
            url: 'Manage/Equipment/libraryList',
            data: {
                page: that.data.page,
                pageSize,
                start_time: that.data.conditions.startTime,
                end_time: that.data.conditions.endTime,
                status: that.data.conditions.statusIndex !== "" ? that.data.statusOptions[that.data.conditions.statusIndex].value : '',
                equipment_number: that.data.conditions.eqSn,
                mainboard_type_number: that.data.conditions.mainboardIndex !== "" ? that.data.mainboardOptions[that.data.conditions.mainboardIndex].number : '',
                partner_id: that.data.conditions.partnerIndex !== "" ? that.data.partners[that.data.conditions.partnerIndex].id : '',
            },
            success: function (res) {
                wx.hideLoading()
                if (res.data.code == 1000) {
                    const source = res.data.data;
                    let libraryList = that.data.libraryList.concat(source.libraryList);
                    that.setData({
                        libraryList: libraryList,
                        total: source.page.total_number,
                        empty: source.page.page_count == 0 ? true : false,
                        loadmore: libraryList.length < source.page.total_number ? true : false,
                        nomore: libraryList.length >= source.page.total_number ? true : false,
                        drawer: false,
                    })
                }
            },
            error: e => {
                wx.hideLoading()
            }
        })
    },

    addLibrary() {
        wx.navigateTo({
            url: "./addLibrary",
        })
    },

})